package com.personal.controller;

import com.personal.bo.AddResourcesBO;
import com.personal.bo.CommentListBO;
import com.personal.bo.NewResourcesByCatBO;
import com.personal.utils.config.MinIoClientConfig;
import com.personal.service.ResourcesService;
import com.personal.utils.MinIoUtils;
import com.personal.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@RequestMapping("/resources")
@Api(tags = "资源文章相关接口")
public class ResourcesController {

    @Autowired
    private MinIoClientConfig minIoClientConfig;

    @Autowired
    private ResourcesService resourcesService;


    @GetMapping("/queryCommonLevelCount")
    @ApiOperation("查询评价等级数量")
    public Result queryCommonLevelCount(@RequestParam("id") Integer id){
        return resourcesService.queryCommonLevelCount(id);
    }

    @GetMapping("/queryResourcesCommentList")
    @ApiOperation("查询资源评价列表")
    public Result queryResourcesCommentList(@Valid CommentListBO commentListBO){
        return resourcesService.queryResourcesCommentList(commentListBO);
    }

    @GetMapping("/queryNewResourcesByCatAndLimit")
    @ApiOperation("通过参数查询最新资源文章列表")
    public Result queryNewResourcesByCatAndLimit(NewResourcesByCatBO newResourcesByCatBO){
         return resourcesService.queryNewResourcesByCatAndLimit(newResourcesByCatBO);
    }

    @PostMapping("/addResources")
    @ApiOperation("添加资源文章（后台接口）")
    public Result addResources(@RequestPart("resourcesImgs") MultipartFile[] resourcesImgs,
                               @RequestPart("addResourcesBO") AddResourcesBO addResourcesBO) throws Exception {

        return resourcesService.addResources(resourcesImgs,addResourcesBO);
    }


    @PostMapping("/upload")
    @ApiOperation("测试文件上传")
    public String testUpload(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        MinIoUtils.uploadFile(minIoClientConfig.getBucketName(), fileName, file.getInputStream());
        String imgUrl = minIoClientConfig.getEndpoint() + "/" + minIoClientConfig.getBucketName() + "/" + fileName;

        return imgUrl;
    }

}
